package online.afeibaili.storebackendframework.controller;

import online.afeibaili.storebackendframework.model.Product;
import online.afeibaili.storebackendframework.server.ProductServer;
import online.afeibaili.storebackendframework.server.StatServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/admin/product")
public class ProductAdminController {
    Logger log = LoggerFactory.getLogger(ProductAdminController.class);
    private final ProductServer productServer;
    private final StatServer statServer;

    public ProductAdminController(ProductServer productServer, StatServer statServer) {
        this.productServer = productServer;
        this.statServer = statServer;
    }

    @PostMapping("/insert")
    public ResponseEntity<Map<String, Object>> insert(
            @RequestPart("productInfo") Map<String, String> productInfo,
            @RequestPart(value = "images", required = false) MultipartFile[] images,
            @RequestPart(value = "videos", required = false) MultipartFile[] videos) {
        Product product = new Product();
        product.setCreateTime(new Date().getTime());
        product.setName(productInfo.get("name"));
        product.setPrice(Double.parseDouble(productInfo.get("price")));
        product.setDescription(productInfo.get("description"));
        product.setDetail(productInfo.get("detail"));
        product.setStatus(productInfo.get("status"));
        product.setCategory(productInfo.get("category"));
        try {
            product.setSales(Integer.parseInt(productInfo.get("sales")));
        } catch (
                Exception e) {
            return ResponseEntity.ok(Map.of("code", 200, "result", "请输入正确的销量"));
        }
        product.setRemark(productInfo.get("remark"));
        log.info("product:{}", product);

        // 直接传递 MultipartFile 对象给 service 处理
        return ResponseEntity.ok(Map.of("code", 200, "result", productServer.insert(product, images, videos)));
    }
    //更新销量
    @PostMapping("/updateSales")
    public ResponseEntity<Map<String, Object>> update(@RequestBody Map<String, String> productInfo) {
        long createTime = Long.parseLong(productInfo.get("createTime"));
        int sales = Integer.parseInt(productInfo.get("sales"));
        return ResponseEntity.ok(Map.of("code", 200, "result", productServer.updateSaleAdmin(createTime, sales), "message", "更新成功"));
    }
    //删除
    @PostMapping("/delete")
    public ResponseEntity<Map<String, Object>> delete(@RequestBody Map<String, String> productInfo) {
        long createTime = Long.parseLong(productInfo.get("createTime"));
        return ResponseEntity.ok(Map.of("code", 200, "result", productServer.delete(createTime)));
    }
    //统计
    @PostMapping("/stat")
    public ResponseEntity<Map<String, Object>> stat(@RequestBody Map<String, String> statInfo) {
        long startTime = Long.parseLong(statInfo.get("startTime"));
        long endTime = Long.parseLong(statInfo.get("endTime"));
        return ResponseEntity.ok(Map.of("code", 200, "data", statServer.selectStat(startTime, endTime)));
    }
    //删除图片
    @PostMapping("/deleteImage")
    public ResponseEntity<Map<String, Object>> deleteImage(@RequestBody Map<String, String> productInfo) {
        String image = productInfo.get("image");
        long createTime = Long.parseLong(productInfo.get("createTime"));
        return ResponseEntity.ok(Map.of("code", 200, "result", productServer.deleteImage(image, createTime)));
    }
    //删除视频
    @PostMapping("/deleteVideo")
    public ResponseEntity<Map<String, Object>> deleteVideo(@RequestBody Map<String, String> productInfo) {
        String video = productInfo.get("video");
        long createTime = Long.parseLong(productInfo.get("createTime"));
        return ResponseEntity.ok(Map.of("code", 200, "result", productServer.deleteVideo(video, createTime)));
    }
    //添加图片
    @PostMapping("/addImage")
    public ResponseEntity<Map<String, Object>> addImage(@RequestPart("image") MultipartFile image, @RequestPart("createTime") String createTime) {
        long createTimeLong = Long.parseLong(createTime);
        return ResponseEntity.ok(Map.of("code", 200, "result", productServer.addImage(image, createTimeLong)));
    }
    //添加视频
    @PostMapping("/addVideo")
    public ResponseEntity<Map<String, Object>> addVideo(@RequestPart("video") MultipartFile video, @RequestPart("createTime") String createTime) {
        long createTimeLong = Long.parseLong(createTime);
        return ResponseEntity.ok(Map.of("code", 200, "result", productServer.addVideo(video, createTimeLong)));
    }
    @GetMapping("/count")
    public ResponseEntity<Map<String, Object>> count() {
        return ResponseEntity.ok(Map.of("code", 200, "result", productServer.count()));
    }
}